package com.bjsxt.usersCenter.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.usersCenter.common.util.ConstatFinalUtil;
import com.bjsxt.usersCenter.common.util.PageInfoUtil;
import com.bjsxt.usersCenter.system.dao.IARegionDao;
import com.bjsxt.usersCenter.system.pojo.ARegion;
import com.bjsxt.usersCenter.system.service.ISystemService;
@Service("systemService")
public class SystemServiceImpl implements ISystemService
{
	@Resource
	private IARegionDao regionDao;
	@Override
	public ARegion findOneRegionService(Map<String, Object> condMap)
	{
		
		int i=1;
		return this.regionDao.findOne(condMap);
	}
	@Override
	public JSONObject updateOneRegionService(ARegion Region)
	{
		int res=this.regionDao.update(Region);
		JSONObject resultJSON=new JSONObject();
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", Region.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","2");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneRegionService(ARegion Region)
	{
		int res=this.regionDao.delete(Region);
		JSONObject resultJSON=new JSONObject();
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("id", Region.getId());
		dataJSON.put("effect", res);
		resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","3");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject saveOneRegionService(ARegion Region)
	{
		int res=this.regionDao.save(Region);
		JSONObject resultJSON=new JSONObject();
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("id", Region.getId());
		
		
		if(res>0)
		{
			resultJSON.put("code","0");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", Region.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
		}else{
			
			resultJSON.put("code","1");
			resultJSON.put("info",ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	@Override
	public List<ARegion> findCondListRegionService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if(condMap.get("keyword")!=null&&!"".equals(condMap.get("keyword"))){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil!=null){
			Map<String, Object> countMap=new HashMap<String,Object>();
			countMap.put("page", "true");
			
			List<ARegion> list=this.regionDao.findCondList(condMap);
			int totalRecord=0;
			if(list.size()>0)
			{
				totalRecord=list.get(0).getId();
			}
			
		
			pageInfoUtil.setTotalRecord(totalRecord);
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageCount", "false");
			condMap.put("pageSize", pageInfoUtil.getPageSize());
		}
		return this.regionDao.findCondList(condMap);
	}
	@Test
	public void test1()
	{
		Map<String,Object> condMap=new HashMap<String, Object>();
		
		condMap.put("parentId", 0);
		
		
		List<ARegion> childRegionList=this.findCondListRegionService(null, condMap);
		ConstatFinalUtil.LOGGER.info(childRegionList);
		
	}
	@Override
	public List<ARegion> findTreeRegionService(ARegion region)
	{
		List<ARegion> regionList=new ArrayList<ARegion>();
		
		if(region!=null)
		{
			regionList.add(region);
			this.findTreeRegionServiceUtil(regionList,region);
			
			Collections.reverse(regionList);
			
			StringBuffer ress=new StringBuffer();
			for (ARegion aRegion : regionList)
			{
				ress.append(aRegion.getName()+",");
			}
			
			if(!"".equalsIgnoreCase(ress.toString()))
			{
				ress.substring(0, ress.length());
			}
			region.setTreeName(ress.toString());
			
			
		}
		
		
		
		
		return regionList;
	}
	private void findTreeRegionServiceUtil(List<ARegion> regionList, ARegion region)
	{
		if(region==null)
		{
			return;
		}
		if(region.getParentId()!=0){
			Map<String,Object> condMap=new HashMap<String, Object>();
		
			condMap.put("id", region.getParentId());
			ARegion regionRes=this.findOneRegionService(condMap);
			regionList.add(regionRes);
			findTreeRegionServiceUtil(regionList, regionRes);
			
		
		}
	}

	
}
